import request from "/utils/omsRequest";
import { moment } from "/utils/core";
import { bus } from "/utils/core";
import { contractType } from "/utils/dictionary";
const app = getApp();

Page({
  data: {
    activeIndex: null,
    pageSize: 20,
    permissions: [],
    visibleFreeServiceCheck: false,
    visibleProgress: false,
    form: {
      merchantName: ""
    },
    contractType,
    contract: {
      total: 0,
      list: []
    }
  },
  onLoad() {

  },
  onShow() {
    this.setData({
      permissions: app.globalData.permissions
    });
    console.log(this.data.permissions.includes(471))
    this.queryData({ pageNumber: 1 });
  },
  moment,
  // 创建合同
  onCreate() {
    dd.navigateTo({
      url: '/pages/contract/findMerchant/findMerchant'
    })
  },
  onReachBottom(e) {
    // 页面被拉到底部
    if (this.data.contract.total !== this.data.contract.list.length) {
      this.queryData({
        ...this.data.form,
        pageNumber: this.data.contract.list.length / this.data.pageSize + 1
      });
    }
  },
  onPullDownRefresh() {
    this.queryData({ ...this.data.form, pageNumber: 1 }).then(() => {
      dd.stopPullDownRefresh();
    })
  },
  onSearch(e) {
    Object.assign(this.data.form, { pageNumber: 1, merchantName: e.detail.value });
    this.queryData({ ...this.data.form, pageNumber: 1 });
  },
  //查询数据
  queryData(params) {
    return new Promise((resolve) => {
      if (params.pageNumber === 1) {
        this.setData({
          contract: {
            total: 0,
            list: []
          }
        })
      }
      dd.showLoading({
        content: '加载中...',
      });
      request({
        url: "/ajax/contract/list",
        data: {
          ...params,
          pageSize: this.data.pageSize
        }
      }).then(res => {
        dd.hideLoading();
        if (this.data.contract.total === 0) {
          this.data.contract.total = res.totalNum;
          this.data.contract.list = res.voList;
        } else {
          this.data.contract.list.push(...res.voList);
        }
        this.setData({
          contract: this.data.contract
        });
        resolve()
      })
    })
  },
  // 查看审核进度
  viewProcess(e) {
    if (e.target.dataset.show) {
      bus.publish("onContractProgress", this.data.contract.list[e.target.dataset.index])
    }
    this.setData({
      activeIndex: null,
      visibleProgress: e.target.dataset.show
    });
  },
  // 查看详情
  jumpToInfo(e) {
    dd.navigateTo({
      url: `/pages/contract/contractInfo/contractInfo?id=${e.target.dataset.id}`
    })
    this.onHideMore()
  },
  onShowMore(e) {
    this.setData({
      activeIndex: e.target.dataset.index
    })
  },
  onHideMore() {
    this.setData({
      activeIndex: null
    })
  },
  // 修改合同
  onModifyContract(e) {
    dd.navigateTo({
      url: `/pages/contract/modifyContract/modifyContract?id=${e.target.dataset.id}`
    })
    this.onHideMore()
  },
  // 查看增值服务审核
  viewFreeServiceCheck(e) {
    if (e.target.dataset.show) {
      bus.publish("onFreeServiceCheckInfo", this.data.contract.list[this.data.activeIndex])
    }
    this.setData({
      activeIndex: null,
      visibleFreeServiceCheck: e.target.dataset.show
    });
  }
});
